Mailing = {
	funds:{}, // contains queried objects, sorted by fundID
	init:function(fundID){
		if(true){
			Mailing.funds[fundID] = {};
			var $fund = $('#fund_'+fundID);
			Mailing.funds[fundID].emails = {};
			Mailing.funds[fundID].$table = $fund.find('.mailing-list-table');
			Mailing.funds[fundID].$email = Mailing.funds[fundID].$table.find('[name=list-email]');
			Mailing.funds[fundID].$name = Mailing.funds[fundID].$table.find('[name=list-name]');
			Mailing.funds[fundID].$thead = Mailing.funds[fundID].$table.find('thead');
			Mailing.funds[fundID].$tfoot = Mailing.funds[fundID].$table.find('tfoot');
			Mailing.funds[fundID].$tbody = Mailing.funds[fundID].$table.find('tbody');
			Mailing.funds[fundID].$filters = Mailing.funds[fundID].$thead.find('tr.filters');
			Mailing.funds[fundID].$sort = Mailing.funds[fundID].$thead.find('tr.sort');
			
			// compose elements
			Mailing.funds[fundID].$compose = $fund.find('.composeFilter');
			Mailing.funds[fundID].$composeSend = Mailing.funds[fundID].$compose.find(".buttonSend");
			Mailing.funds[fundID].$mailingID = Mailing.funds[fundID].$compose.find("[name=mailingID_"+fundID+"]");
			Mailing.funds[fundID].$from = Mailing.funds[fundID].$compose.find("[name=from_"+fundID+"]");
			Mailing.funds[fundID].$mailingName = Mailing.funds[fundID].$compose.find("[name=name_"+fundID+"]");
			Mailing.funds[fundID].$subject = Mailing.funds[fundID].$compose.find("[name=subject_"+fundID+"]");
			Mailing.funds[fundID].$body = Mailing.funds[fundID].$compose.find("[name=body_"+fundID+"]");
			
			// buttons that change states
			Mailing.funds[fundID].$buttonChecked = Mailing.funds[fundID].$compose.find(".buttonSend");
			$.merge(Mailing.funds[fundID].$buttonChecked,Mailing.funds[fundID].$tfoot.find('button.tag,button.delete'));
			Mailing.funds[fundID].$buttonAll = Mailing.funds[fundID].$tfoot.find(".selectAll");
			Mailing.funds[fundID].$buttonNone = Mailing.funds[fundID].$tfoot.find(".deselect");
			Mailing.funds[fundID].$buttonUntag = Mailing.funds[fundID].$tfoot.find(".untag");
			
			Mailing.funds[fundID].$email.on('keydown',function(e){
				Mailing.keyup(fundID,e);
			});
			Mailing.funds[fundID].$name.on('keydown',function(e){
				Mailing.keyup(fundID,e);
			});	
		}
		//populate the list of emails for this fund
		Mailing.funds[fundID].$tbody.find('tr').not('.hidden').each(
			function(){
				var $this = $(this);
				var email = $this.find('.email').find('a').text().toLowerCase();
				if(email in Mailing.funds[fundID].emails){
					console.log(email + ' already in struct');
				}
				Mailing.funds[fundID].emails[email] = 1;
			}
		);
		Mailing.funds[fundID].checked = {};
		Mailing.funds[fundID].checkedTags = {};
	},
	createRow:function(email,name,fundID){
		var $clone = Mailing.funds[fundID].$thead.find('tr:last').clone();
		
		Mailing.funds[fundID].emails[email.toLowerCase()] = 1;
		$clone.removeClass('hidden');
		$clone.find('td.email').find('a').text(email);
		$clone.find('td.name').find('a').text(name);
		$clone.find('[name=edit-email]').val(email);
		$clone.find('[name=edit-name]').val(name);
		$clone.attr('email',email.toLowerCase());
		Mailing.funds[fundID].$tbody.find('tr.no_contacts').remove();
		Mailing.funds[fundID].$tbody.append($clone);
		Mailing.funds[fundID].$tbody.animate({ scrollTop: Mailing.funds[fundID].$tbody[0].scrollHeight}, 700);
		Mailing.oddEven(fundID);
		$(document).trigger("document:changed");
	},
	saveContact:function(id,email,name,fundID){
		var id = id || "";
		if(email.length > 0 && _Valid._email(email)){
			if(email.toLowerCase() in Mailing.funds[fundID].emails && id == ""){ // email exists
				alert('email exists');
			} else {
				Ajax.run("fundService.saveContact.test",
					function(data){
						var $tr = [];
						if(id == ""){
							$tr = Mailing.getRowByEmail(fundID,data.EMAIL);
						} else {
							$tr = Mailing.getRowByID(fundID,id);
						}
						$tr.find('input[type=checkbox]').removeClass('hidden');
						$tr.find('input[type=checkbox]').val(data.ID);
						$tr.find('img').hide();
						$tr.attr('email',data.EMAIL.toLowerCase());
						Mailing.evaluateButtons(fundID);
					},
					{contactID:id,name:name,email:email,fundID:fundID}
				);
			}
		}
	},
	keyup:function(fundID,e){
		if(e.keyCode == 13  && Valid.validate(Mailing.funds[fundID].$tfoot)){
			var email = Mailing.funds[fundID].$email.val().toLowerCase();
			var name = Mailing.funds[fundID].$name.val();
			if(email in Mailing.funds[fundID].emails){
				Mailing.showDupe(fundID,email);
			} else {
				Mailing.saveContact('',email,name,fundID);
				Mailing.createRow(email,name,fundID);
				Mailing.funds[fundID].$email.val('');
				Mailing.funds[fundID].$name.val('');
				Mailing.funds[fundID].$email.focus();
			}
		}
	},
	showDupe:function(fundID,email){
		var $matchTR = Mailing.getRowByEmail(fundID,email);
		Mailing.funds[fundID].$tbody.animate({
			scrollTop:($matchTR.position().top + Mailing.funds[fundID].$tbody.scrollTop()) - (Mailing.funds[fundID].$tbody.height() /2)
		},500);
		$matchTR.find('a.editable').css('color','orange');
		setTimeout(function(){
			$matchTR.find('a.editable').css('color','');
		},3000);
	},
	getRowByEmail:function(fundID,email){
		var escapedEmail = email.toLowerCase();
		escapedEmail = escapedEmail.replace(/\./g,"\\.");
		escapedEmail = escapedEmail.replace(/\@/g,"\\@");
		escapedEmail = escapedEmail.replace(/\$/g,"\\$");
		return Mailing.funds[fundID].$table.find('tr[email='+escapedEmail+']:first');
	},
	getRowByID:function(fundID,id){
		return Mailing.funds[fundID].$tbody.find('input[value='+id+']').closest('tr');
	},
	showEdit:function(that){
		var $that = $(that);
		var $tr = $that.closest('tr');
		var $fund = $tr.closest('.fund');

		var fundID = $fund.attr('id').substring(5,$fund.attr('id').length);
		var which = "";
		
		if($that.parent().hasClass('email')){
			which = 'email';
		} else {
			which = 'name';
		}
		var nameWidth = Mailing.funds[fundID].$tbody.find('.name:first').width();
		var emailWidth = Mailing.funds[fundID].$tbody.find('.email:first').width();
		var $email = $tr.find('input[name=edit-email]');
		var $name = $tr.find('input[name=edit-name]');
		$email.attr('style','width:'+(emailWidth-10)+'px !important');
		$name.attr('style','width:'+(nameWidth-10)+'px !important');
		if(which == 'email'){
			$tr.find('td.email .editable').hide();
			$email.removeClass('hidden');
			$email.focus();
		} else {
			$tr.find('td.name .editable').hide();
			$name.removeClass('hidden');
			$name.focus();
		}
	},
	saveEdit:function(that,keyCode){
		var keyCode = keyCode || 13;
		if(keyCode == 13){
			var $tr = $(that).closest('tr');
			var oldEmail = $tr.attr('email');
			var id = $tr.find('input[type=checkbox]').val();
			var email = $tr.find('input[name=edit-email]').val();
			var name = $tr.find('input[name=edit-name]').val();
			var fundID = $tr.closest('table').attr('fundID');
			var $a_email = $tr.find('td.email a');
			var $a_name = $tr.find('td.name a');
			if(Valid.validate($tr)){
				delete Mailing.funds[fundID].emails[oldEmail];
				Mailing.funds[fundID].emails[email] = 1;
				Mailing.saveContact(id,email,name,fundID);
				$tr.find('.saving').show();
				$tr.find('input[type=checkbox]').addClass('hidden');
				$tr.find('.email-row-input').addClass('hidden');
				$tr.find('a.editable').show();
				$a_email.text(email);
				if(name != ''){
					$a_name.text(name);
				} else {
					$a_name.html('<span style="color:grey;">- no name -</span>');
				}
				Mailing.funds[fundID].$email.focus();
			}
		}
	},
	checkToggle:function(that,fundID){
		if($(that).is(':checked')){
			Mailing.checked(that,fundID);
		} else {
			Mailing.unchecked(that.value,fundID);
		}
	},
	checked:function(that,fundID,skipEval){
		var skipEval = skipEval || false;
		if(that.value in Mailing.funds[fundID].checked){
			console.warn('already exists!' + that.value);
		}
		Mailing.funds[fundID].checked[that.value] = $(that).closest('tr');
		if(+Mailing.funds[fundID].checked[that.value].attr('hastag') > 0){
			Mailing.funds[fundID].checkedTags[that.value] = 1;
		}
		if(!skipEval){
			Mailing.evaluateButtons(fundID);
		}
	},
	unchecked:function(value,fundID,skipEval){
		var skipEval = skipEval || false;
		delete Mailing.funds[fundID].checked[value];
		delete Mailing.funds[fundID].checkedTags[value];
		if(!skipEval){
			Mailing.evaluateButtons(fundID);
		}
	},
	deleteEmails:function(that,fundID){
		if(!$(that).hasClass('disabled') && confirm('Delete '+ Object.size(Mailing.funds[fundID].checked) + ' Contact(s)?')){
			var ids = "";
			var idArray = [];
			var rows = {};
			for(key in Mailing.funds[fundID].checked){
				Mailing.funds[fundID].checked[key].fadeOut(300);
				if(ids.length > 0){
					ids += ",";
				}
				ids += key;
				idArray.push(key);
				rows[key] = Mailing.funds[fundID].checked[key];
				var email = Mailing.funds[fundID].checked[key].attr('email').toLowerCase();
				Mailing.unchecked(key,fundID);
				delete Mailing.funds[fundID].emails[email];
			}
			Ajax.run("fundService.deleteContacts.test",
				function(data){
					for(var i = 0; i < idArray.length; i++){
						// add try around this
						rows[idArray[i]].remove();
					}
				},
				{contactIDs:ids}
			);
			setTimeout(function(){
				Mailing.oddEven(fundID);
			},400); // 400 so the 300 fade is done
		}
	},
	oddEven:function(fundID){
		var iterator = 0;
		Mailing.funds[fundID].$tbody.find('tr:visible').each(function(){
			iterator++;
			if(iterator % 2 == 1){
				$(this).addClass('odd');
			} else {
				$(this).removeClass('odd');
			}
		});
	},
	showFilters:function(fundID){
		Mailing.funds[fundID].$sort.hide();
		Mailing.funds[fundID].$filters.fadeIn(300);
		Mailing.adjustFilterHeight(fundID);
	},
	showSort:function(fundID){
		Mailing.funds[fundID].$filters.hide();
		Mailing.funds[fundID].$sort.fadeIn(300);
		Mailing.adjustFilterHeight(fundID);
		setTimeout(function(){
			Mailing.funds[fundID].$sort.fadeOut(function(){
				Mailing.adjustFilterHeight(fundID);
			});
		},5000);
	},
	changeSort:function(fundID){
		Mailing.funds[fundID].$sort.fadeOut(function(){
			Mailing.adjustFilterHeight(fundID);
		});
		Fund.throttle(Mailing.refreshMailingList,'Mailing',700,[fundID]);
	},
	refreshMailingList:function(fundID){
		var formData = Fundr.getAjaxData(Mailing.funds[fundID].$filters);
		formData.fundID = fundID;
		formData.sort = Radio.get('sort_'+fundID);
		formData.order = Radio.get('order_'+fundID);
		Ajax.run("fundView.renderMailingListRows",
			function(data){
				Mailing.funds[fundID].$tbody.fadeOut(function(){
					Mailing.funds[fundID].$tbody.html(data);
					Mailing.funds[fundID].$tbody.fadeIn(function(){
						delete Mailing.funds[fundID];
						Mailing.init(fundID);
						Mailing.evaluateButtons(fundID);
					});
				});
				// check to see if any filters are selected. if not, hide the filters
				var hasFilter = false;
				var formData = Fundr.getAjaxData(Mailing.funds[fundID].$filters);
				for(var i in formData){
					if(formData[i] != ""){
						hasFilter = true;
					}
				}
				if(!hasFilter){
					Mailing.funds[fundID].$filters.fadeOut(function(){
						Mailing.adjustFilterHeight(fundID);
					});
				}
			},
			formData
		);
	},
	showCompose:function(fundID){
		var $fund = $('#fund_'+fundID);
		$fund.find('.historyFilter').hide();
		$fund.find('.composeFilter').css('display','inline-block');
	},
	showHistory:function(fundID){
		var $fund = $('#fund_'+fundID);
		$fund.find('.composeFilter').hide();
		$fund.find('.historyFilter').css('display','inline-block');
	},
	saveMailing:function(fundID,that){
		var $this = $(that);
		if(!$this.hasClass('disabled') && Valid.validate(Mailing.funds[fundID].$compose)){
			var $fund = $('#fund_'+fundID);
			$this.addClass('disabled');
			$this.text('saving...');
			
			//stop nameswap
			NameSwap.stop(fundID);
			Ajax.run("mailingService.saveMailing.test",
				function(data){
					$this.removeClass('disabled');
					$this.text('save');
					Mailing.refreshMailingSelect(fundID,data);
					Mailing.refreshFilters(fundID);
					
					//start after it saves
					NameSwap.start(fundID);
				},
				{
					fundID:fundID,
					id:Mailing.funds[fundID].$compose.find('[name=mailingID_'+fundID+']').val(),
					name:Mailing.funds[fundID].$mailingName.val(),
					subject:Mailing.funds[fundID].$subject.val(),
					from:Mailing.funds[fundID].$from.val(),
					body:Mailing.funds[fundID].$body.val()
				}
			);
		}
	},
	deleteMailing:function(fundID,that){
		var $this = $(that);
		if(!$this.hasClass('disabled') && confirm('Delete this Mailing?')){
			var $fund = $('#fund_'+fundID);
			$this.addClass('disabled');
			$this.text('deleting...');
			
			Ajax.run("mailingService.deleteMailing.test",
				function(data){
					$this.removeClass('disabled');
					$this.text('delete');
					Mailing.refreshMailingSelect(fundID);
					Mailing.refreshFilters(fundID);
				},
				{mailingID:$('[name=mailing_'+fundID+']').val()}
			);
		}
	},
	changeMailing:function(fundID){
		var $select = $('[name=mailing_'+fundID+']');
		var mailingID = $select.val();
		Valid.removeMessages(Mailing.funds[fundID].$compose);
		Mailing.funds[fundID].$compose.find('.validation_failed').removeClass('validation_failed');
		if(mailingID != ''){
			Mailing.funds[fundID].$mailingID.val(mailingID);
			Mailing.funds[fundID].$from.val(Compose.funds[fundID].mailings[mailingID].from);
			Mailing.funds[fundID].$mailingName.val(Compose.funds[fundID].mailings[mailingID].name);
			Mailing.funds[fundID].$subject.val(Compose.funds[fundID].mailings[mailingID].subject);
			Mailing.funds[fundID].$body.val(Compose.funds[fundID].mailings[mailingID].body);
			Mailing.funds[fundID].$compose.find('.buttonDelete').removeClass('disabled');
		} else {
			Mailing.funds[fundID].$mailingID.val('');
			Mailing.funds[fundID].$mailingName.val('');
			Mailing.funds[fundID].$subject.val('');
			Mailing.funds[fundID].$body.val('');
			Mailing.funds[fundID].$compose.find('.buttonDelete').addClass('disabled');
		}
	},
	refreshMailingSelect:function(fundID,id){
		Ajax.run("mailingView.renderSelect",
			function(data){
				$('[name=mailing_'+fundID+']').replaceWith(data);
			},
			{fundID:fundID,mailingID:id}
		);
	},
	selectAll:function(fundID,that){
		if(!$(that).hasClass('disabled')){
			Mailing.funds[fundID].$tbody.find('input[type=checkbox]').each(function(){
				$(this).prop('checked',true);
				Mailing.checked(this,fundID,true);
			});
			Mailing.evaluateButtons(fundID);
		}
	},
	selectNone:function(fundID,that){
		if(!$(that).hasClass('disabled')){
			Mailing.funds[fundID].$tbody.find('input[type=checkbox]').prop('checked',false);
			Mailing.funds[fundID].checked = {};
			Mailing.funds[fundID].checkedTags = {};
			Mailing.evaluateButtons(fundID);
		}
	},
	evaluateButtons:function(fundID){
		var total = Object.size(Mailing.funds[fundID].emails);
		var checked = Object.size(Mailing.funds[fundID].checked);
		var checkedTags = Object.size(Mailing.funds[fundID].checkedTags);

		if(total == 0){
			Mailing.funds[fundID].$buttonChecked.addClass('disabled');
			Mailing.funds[fundID].$buttonAll.addClass('disabled');
			Mailing.funds[fundID].$buttonNone.addClass('hidden');
		} else {
			if(checked > 0){
				Mailing.funds[fundID].$buttonChecked.removeClass('disabled');
				Mailing.funds[fundID].$buttonNone.removeClass('hidden');
				
				if(checked < total){
					Mailing.funds[fundID].$buttonAll.removeClass('disabled');
				} else {
					Mailing.funds[fundID].$buttonAll.addClass('disabled');
				}
				NameSwap.start(fundID);
			} else {
				NameSwap.stop(fundID);
				Mailing.funds[fundID].$buttonChecked.addClass('disabled');
				Mailing.funds[fundID].$buttonAll.removeClass('disabled');
				Mailing.funds[fundID].$buttonNone.addClass('hidden');
			}
			if(checkedTags > 0){
				Mailing.funds[fundID].$buttonUntag.removeClass('hidden');
			} else {
				Mailing.funds[fundID].$buttonUntag.addClass('hidden');
			}
		}
	},
	openTagModal:function(fundID,that){
		if(!$(that).hasClass('disabled')){
			var size = Object.size(Mailing.funds[fundID].checked);
			var title = "Tag "+size+" Contact";
			if(size > 1){
				title += "s";
			}
			Modal.callback.tag_form = function(){
				Mailing.loadContactTags(fundID);
				Modal.$.find('.theTitle').text(title);
				Modal.$.find('[name=contactTagfundID]').val(fundID);
				Modal.$.find('[name=contactTag]').val('');
				Modal.$.find('[name=contactTag]').on('keydown',function(e){
					if(e.keyCode == 13){
						Mailing.saveTags();
					}
				});
			};
			Modal.open('tag_form');
		}
	},
	saveTags:function(){
		var fundID = Modal.$.find('[name=contactTagfundID]').val();
		var size = Object.size(Mailing.funds[fundID].checked);
		var tag = Modal.$.find('[name=contactTag]').val();
		if(true){
			var mailingIDs = "";
			for(var id in Mailing.funds[fundID].checked){
				if(mailingIDs != ""){
					mailingIDs+=",";
				}
				mailingIDs+=id;
			}
			Modal.close();
			Ajax.run("mailingService.tagContacts.test",
				function(data){
					Mailing.refreshMailingList(fundID);
					Mailing.refreshFilters(fundID);
				},
				{fundID:fundID,mailingIDs:mailingIDs,tag:tag}
			);
		}
	},
	clearTags:function(fundID){
		var size = Object.size(Mailing.funds[fundID].checked);
		if(confirm("Clear all tags from "+size+" contact(s)?")){
			var mailingIDs = "";
			for(var id in Mailing.funds[fundID].checkedTags){
				if(mailingIDs != ""){
					mailingIDs+=",";
				}
				mailingIDs+=id;
			}
			Ajax.run("mailingService.clearContactTags.test",
				function(data){
					Mailing.refreshFilters(fundID,true);
				},
				{mailingIDs:mailingIDs,fundID:fundID}
			);
		}
	},
	loadContactTags:function(fundID){
		Ajax.run("mailingService.getFundContactTagsByID.test",
			function(data){
				var $tag = Modal.$.find('.contactTag.hidden');
				var array = data.split(',');
				if(data == ''){
					array = [];
				}
				for(var i = 0; i < array.length; i++){
					var $newTag = $tag.clone();
					$newTag.removeClass('hidden');
					$newTag.text(array[i]);
					$tag.after($newTag);
				}
			},
			{fundID:fundID}
		);
	},
	fillTag:function(tag){
		var $input = Modal.$.find('[name=contactTag]');
		$input.val(tag.toLowerCase());
	},
	toggleGiven:function(fundID,that){
		if($(that).is(':checked')){
			var other = (+that.value%2)+1;
			$('#filter_'+fundID+'_'+other).prop('checked',false);
			$('#checkboxes_filter_'+fundID).find('li[value='+other+']').removeClass('checked');
		}
		Fund.throttle(Mailing.refreshMailingList,'Mailing',700,[fundID]);
	},
	toggleTags:function(fundID){
		Mailing.adjustFilterHeight(fundID);
		Fund.throttle(Mailing.refreshMailingList,'Mailing',700,[fundID]);
	},
	adjustFilterHeight:function(fundID){
		var normalHeight = 156;
		var $fund = $('#fund_'+fundID);
		var $filters = $fund.find('tr.filters');
		var $sort = $fund.find('tr.sort');
		var filterHeight = $filters.height();
		var sortHeight = $sort.height();
		if(!$filters.is(':visible')){
			filterHeight = 0;
		}
		if(!$sort.is(':visible')){
			sortHeight = 0;
		}
		var height = sortHeight + filterHeight;
		Mailing.funds[fundID].$tbody.css('max-height',(normalHeight - height) + 'px');
	},
	refreshFilters:function(fundID,refreshList){
		var refreshList = refreshList || false;
		Ajax.run("fundView.renderFilters",
			function(data){
				var showFilters = (Mailing.funds[fundID].$filters.is(':visible'));
				var showSort = (Mailing.funds[fundID].$sort.is(':visible'));
				var formData = Fundr.getAjaxData(Mailing.funds[fundID].$filters);
				Mailing.funds[fundID].$thead.html(data);
				Mailing.funds[fundID].$filters = Mailing.funds[fundID].$thead.find('tr.filters');
				Mailing.funds[fundID].$sort = Mailing.funds[fundID].$thead.find('tr.sort');
				if(showFilters){
					Mailing.funds[fundID].$filters.show();
				}
				if(showSort){
					Mailing.funds[fundID].$sort.show();
				}
				for(var key in formData){
					var array = key.split('_');
					if(array[0] == 'filter'){
						Checkbox.set(key,formData[key],true);
					} else {
						TriRadio.set(key,formData[key],true);
					}
				}
				if(refreshList){
					Mailing.refreshMailingList(fundID);
				}
			},
			{fundID:fundID}
		);
	}
};

Compose = {
	funds:{}
};

NameSwap = {
	current:'',
	names:['name'],
	currentName:"name",
	iterator:0,
	inProgress:null,
	start:function(fundID){
		NameSwap.stop(fundID);
		NameSwap.current = fundID;
		NameSwap.populate(fundID);
		NameSwap.iterator = 0;
		NameSwap.currentName = "name";
		NameSwap.next(fundID);
	},
	stop:function(fundID){
		try{clearTimeout(NameSwap.inProgress);}catch(e){/*do nothing*/}
		NameSwap.current = "";
		NameSwap.names = ['name'];
		var $body = Mailing.funds[fundID].$compose.find('[name=body_'+fundID+']');
		var value = $body.val();
		if(value.indexOf('['+NameSwap.currentName+']') >= 0){
			value = value.split('['+NameSwap.currentName+']').join('[name]');
			$body.val(value);
		}
		NameSwap.iterator = 0;
		NameSwap.currentName = 'name';
		
	},
	next:function(fundID){
		var $body = Mailing.funds[fundID].$compose.find('[name=body_'+fundID+']');
		var value = $body.val();
		if(value.indexOf('['+NameSwap.currentName+']') >= 0 && !$body.is(':focus')){
			value = value.split('['+NameSwap.currentName+']').join('['+NameSwap.names[NameSwap.iterator]+']');
			$body.val(value);
			NameSwap.currentName = NameSwap.names[NameSwap.iterator];
			NameSwap.iterator++;
			if(NameSwap.iterator >= NameSwap.names.length){
				NameSwap.iterator = 0;
			}
		}
		if(NameSwap.current != ''){
			NameSwap.inProgress = setTimeout(function(){
				NameSwap.next(fundID);
			},1200);
		}
	},
	populate:function(fundID){
		var limit = 10;
		var iterator = 0;
		var array = [];
		var no_name = false; // track whether or not htere are un-named contacts
		
		if(Object.size(Mailing.funds[fundID].checked) == 0){
			Mailing.funds[fundID].$tbody.find('tr').each(function(){
				if(iterator < limit){
					var name = $(this).find('[name=edit-name]').val() || "";
					if(name.length > 0){
						if(array.indexOf(name) == -1){
							array.push(name);
							iterator++
						}
					} else {
						no_name = true;
					}
				}
			});
		} else {
			for(var $i in Mailing.funds[fundID].checked){
				if(iterator < limit){
					var name = Mailing.funds[fundID].checked[$i].find('[name=edit-name]').val() || "";
					if(name.length > 0){
						if(array.indexOf(name) == -1){
							array.push(name);
							iterator++
						}
					} else {
						no_name = true;
					}
				}
			}
		}

		if(array.length > 0){
			if(no_name && array.indexOf('friend') == -1){
				array.push('friend'); // add single entry for 'friend' when un-named contacts exist
			}
			NameSwap.names = array;
		} else {
			NameSwap.names = ['name'];
		}
	}
};